/*********************************************************************
* File: ioLibrary.cpp
*
* \brief Miscellaneous I/O functions.
*
* \author Instituto Superior Tecnico de Lisboa, Portugal
* \author Technical University of Lisbon, Portugal
* 
* \author Agentes Autonomos e Sistemas Multiagente
* \author Autonomous Agents and Multiagent Systems
* 
* \version	1.0
* \date		2006/2007
*********************************************************************/
#include "ioLibrary.h"
using namespace util;
#include <string.h>

/**
* \brief Reads a line from a file.
**/
bool util::io::getLine(FILE* file, char *buffer)
{
	int index = 0;

	char ch;
	ch = fgetc(file);

	// skip any leading blanks
	while (ch == ' ')
		ch = fgetc(file);
	
	while (ch != EOF && ch != '\r' && ch != '\n')
	{
		if (ch == '\t')  // convert tabs to spaces
			ch = ' ';
		
		buffer[index] = ch;
		ch = fgetc(file);

		// skip multiple spaces in input file
		while (buffer[index] == ' ' && ch == ' ')
			ch = fgetc(file);
		
		index++;
	}

	while (ch == '\r')  // is it a carriage return?
		ch = fgetc(file);  // skip the linefeed

	buffer[index] = 0;  // terminate the command line

	if(strlen(buffer) == 0)
		return false;
	else
		return true;
}